const path = require("path");
const fs = require("fs");
const { promisify } = require("util");
// promisify
const fsWriteFile = promisify(fs.writeFile);
const fsReadFile = promisify(fs.readFile);

export const setTargetConfig = async (targetPath, config) => {
 //  console.log(config, "复制文件夹路径");
 await Promise.all([
  setPackageJson(targetPath, config),
  setReadme(targetPath, config),
 ]);
};

// 修改package.json
const setPackageJson = async (targetPath, configs) => {
 const objPath = path.join(targetPath, "package.json");
 const packages = require(objPath);
 packages.name = configs.name;
 const jsonData = JSON.stringify(packages, null, 2);
 await fsWriteFile(objPath, jsonData);
};

// 修改README.md
const setReadme = async (targetPath, configs) => {
 const objPath = path.join(targetPath, "README.md");
 let data = await fsReadFile(objPath, "utf8");
 data = data.replace("SIM-NAME", configs.name);
 await fsWriteFile(objPath, data);
};

// 修改.env
// const setEnv = async (targetPath, configs) => {
//  let namespace = configs.namespace;
//  // 完善namespace数据
//  if (!namespace.length) {
//   namespace = "/";
//  }
//  if (namespace[0] !== "/") {
//   namespace = "/" + namespace;
//  }
//  if (namespace[namespace.length - 1] !== "/") {
//   namespace = namespace + "/";
//  }
//  // 写入文件
//  const objPath = path.join(targetPath, ".env");
//  let data = await fsReadFile(objPath, "utf8");
//  // 正则中.不包含换行符，这里正好截取一行
//  data = data.replace(/VITE_NAMESPACE.*/g, `VITE_NAMESPACE = ${namespace}`);
//  await fsWriteFile(objPath, data);
// };
